package com.example.mvc;


import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.example.entity.Book;
import com.example.service.BookService;

@Controller
@RequestMapping("/getUsers" )
@SessionAttributes(value={"uname","pwd"})
public class BookMvc {

	@Autowired
	private BookService bookService;
	
	private int aw;
	private Book book;
	private ModelAndView modelAndView;
	private List list;
	
	
	@RequestMapping("/addBook")
	public String addBook(@ModelAttribute("book")Book book){
	  
	    aw=bookService.add(book);
	    return Asd(aw,"/queryBook","/error");
	}
	
	@RequestMapping("/queryBook")
	public ModelAndView query(){
		list=new ArrayList<Book>();
		list=bookService.query();
		modelAndView= new ModelAndView();
		modelAndView.setViewName("queryBook");
		modelAndView.addObject("list",list);
		return modelAndView;
	}
	
	
	@RequestMapping("/deleteBook/{id}")
	public String deleteBook(@ModelAttribute("book")Integer id){
	  
	    aw=bookService.delete(id);
	    return Asd(aw,"/queryBook","/error");
	}
	
	@RequestMapping("/updateBook")
	public String updateBook(@ModelAttribute("book")Book book){
	  
	    aw=bookService.update(book);
	    return Asd(aw,"/queryBook","/error");
	}

	
	public String Asd(int aw,String args1,String args2){
		if(aw==1){
			return args1;
		}else{
			return args2;
		}
		
	}
	
	@RequestMapping("/addBook1")
	public String addBook1(){
		
		return "/addBook";
	}
	
	
}
